﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;


public partial class school_departments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }

            ClearFields();
        }
    }

    protected void btnShowDepartment_Click(object sender, EventArgs e)
    {

    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            DepartmentTable _Department = new DepartmentTable();
            _Department.SchoolKey = Convert.ToInt32(lblSchoolKey.Text);
            _Department.DepartmentKey = Convert.ToInt32(lblDepartmentKey.Text);
            _Department.LocationKey = Convert.ToInt32(lblLocationKey.Text);
            _Department.DepartmentCode = txtDepartmentCode.Text;
            _Department.DepartmentName = txtDepartmentName.Text;
            _Department.DepartmentDescription = txtDescription.Text;
            _Department.Status = chkStatus.Checked;
            int _DepartmentKey = DepartmentTableManager.Save(_Department);
            if (_DepartmentKey > 0)
            {
                ClearFields();
                grvDepartment.DataBind();
                lblMessage.Text = "Saved sussccessfully!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Saving failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlLocation.DataBind();
        ClearFields();
    }

    protected void grvDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        if (grvDepartment.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvDepartment.SelectedDataKey;
            int _DepartmentKey = Convert.ToInt32(_selectedDataKey[0]);
            int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);
            int _locationKey = Convert.ToInt32(_selectedDataKey[2]);
            if (_DepartmentKey > 0 && _schoolKey > 0)
            {
                lblSchoolKey.Text = _schoolKey.ToString();
                lblDepartmentKey.Text = _DepartmentKey.ToString();
                lblLocationKey.Text = _locationKey.ToString();
                GridViewRow _selectedRow = grvDepartment.SelectedRow;
                txtDepartmentCode.Text = _selectedRow.Cells[2].Text;
                txtDepartmentName.Text = _selectedRow.Cells[3].Text;
                txtDescription.Text = _selectedRow.Cells[4].Text;
                //lblMessage.Text = _selectedRow.Cells[4].Text;
                CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[5].Controls[0]);
                chkStatus.Checked = _chkStatus.Checked;
                //lblMessage.Text = _chkStatus.Checked.ToString();
            }
        }
    }

    private void ClearFields()
    {
        lblMessage.Text = "";
        lblSchoolKey.Text = ddlSchoolList.Text;
        lblLocationKey.Text = ddlLocation.Text;
        lblDepartmentKey.Text = "-1";
        txtDepartmentCode.Text = "";
        txtDepartmentName.Text = "";
        txtDescription.Text = "";
        chkStatus.Checked = false;
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblDepartmentKey.Text.Length > 0)
        {
            if (DepartmentTableManager.Delete(Convert.ToInt32(lblDepartmentKey.Text), Convert.ToInt32(ddlSchoolList.Text)))
            {
                ClearFields();
                grvDepartment.DataBind();
                lblMessage.Text = "Item was deleted!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Deleting failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please select an item to delete.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
}
